/* 
 * File:   DescribeTable.cpp
 * Author: vikrantsingh
 * 
 * Created on 2 November, 2013, 9:14 PM
 */

#include "DescribeTable.h"
#include "Validation.h"
#include "../Sysfiles/SysCatalog.h"
#include "../dependency/FieldLengthLookup.h"
#include "../dependency/DataTypeLookup.h"

#include <iomanip>
using namespace std;
DescribeTable::DescribeTable() {
}

DescribeTable::DescribeTable(const DescribeTable& orig) {
}

DescribeTable::~DescribeTable() {
}

STATUS_CODE DescribeTable::run(QueryStructures *queryStruct) {

    int err = Validation::isValid();
    if (err != SUCCESS) {
        return err;
    }
    SysCatalog cat;
   // Schema schema;
    err = cat.getSchema(queryStruct->tableName.c_str(), queryStruct->schema);
    if (err == TABLE_NOT_FOUND) {
        return err;
    }
    queryStruct->schemaDescription = queryStruct->schema.describeSchema();
    cout << queryStruct->tableName<<endl;
    for (int i = 0; i < queryStruct->schema.numOfColumns; i++) {
        cout <<setiosflags(ios::left)<<setw(30)<< queryStruct->schema.columnNames[i] <<setw(15)<<DataTypeLookup::getTypeDesc(queryStruct->schema.fieldTypes[i])<<resetiosflags(ios::left)<<setw(5) << queryStruct->schema.fieldLengths[i]<<endl;

    }

    //cout<<queryStruct->schemaDescription;
    DEBUG("schema Desc" << queryStruct->schemaDescription);
    return SUCCESS;

}